#include "ServerWorldObjects.h"

#include <iostream>

using namespace std;

/*sf::Packet& operator >>(sf::Packet& packet, ServerWorldObjects& ServerWorldObjects) {

	int dir, act;
	packet >> ServerWorldObjects.position.x >> characterClient.position.y 
		>> dir >> act >> characterClient.nbPoints;

	return packet;
}*/

ServerWorldObjects::ServerWorldObjects(sf::TcpSocket* s1, sf::TcpSocket* s2, int nbYellowGems, int nbVioletGems, sf::Vector2f* positionYellow, sf::Vector2f* positionViolet) {
	socketPlayer1 = s1;
	socketPlayer2 = s2;

	yellowGemsNumber = nbYellowGems;
	yellowGemsUpNb = 0;
	yellowGemsUpdate = NULL;

	violetGemsNumber = nbVioletGems;
	violetGemsUpNb = 0;
	violetGemsUpdate = NULL;


	/*Alloue le tableau contenant les gems jaunes et initialise les gems jaunes*/
	yellowGems = (GemServer**)malloc(sizeof(GemServer*) * yellowGemsNumber);
	for(int i=0; i<yellowGemsNumber; i++) {
		yellowGems[i] = new GemServer(GemColour::YELLOW, positionYellow[i].x, positionYellow[i].y, i);  
	}

	/*Alloue le tableau contenant les gems violettes et initialise les gems violettes*/
	violetGems = (GemServer**)malloc(sizeof(GemServer*) * violetGemsNumber);
	for(int i=0; i<yellowGemsNumber; i++) {
		violetGems[i] = new GemServer(GemColour::PURPLE, positionViolet[i].x, positionViolet[i].y, i);  
	}

	firstSend();
}

/*Appelle la methode initialiseB2D de chaque objetdans le monde*/
void ServerWorldObjects::initialisation(b2World* world) {

	for(int i=0; i<yellowGemsNumber; i++) {
		yellowGems[i]->initialiseB2D(world);
	}

	for(int i=0; i<violetGemsNumber; i++) {
		violetGems[i]->initialiseB2D(world);
	}

}

/*Envoie les id des gemmes MAJ depuis le dernier envoie au client*/
void ServerWorldObjects::send() {	

	/*On envoie d'abord le nombre de gems jaunes MAJ*/
	socketPlayer1->send(&yellowGemsUpNb, sizeof(int));
	socketPlayer2->send(&yellowGemsUpNb, sizeof(int));

	/*Envoie ensuite les gems mise a jour*/
	for(int i = 0; i < yellowGemsUpNb; i++) {
		socketPlayer1->send(&yellowGemsUpdate[i], sizeof(int));
		socketPlayer2->send(&yellowGemsUpdate[i], sizeof(int));
	}

	/*On envoie d'abord le nombre de gems violettes MAJ*/
	socketPlayer1->send(&violetGemsUpNb, sizeof(int));
	socketPlayer2->send(&violetGemsUpNb, sizeof(int));
	
	/*Envoie ensuite les gems violettes mise a jour*/
	for(int i = 0; i < violetGemsUpNb; i++) {
		socketPlayer1->send(&violetGemsUpdate[i], sizeof(int));
		socketPlayer2->send(&violetGemsUpdate[i], sizeof(int));
	}

	if(yellowGemsUpdate != NULL) {
		free(yellowGemsUpdate);
		yellowGemsUpdate = NULL;
	}
	if(violetGemsUpdate != NULL) {
		free(violetGemsUpdate);
		violetGemsUpdate = NULL;
	}
	yellowGemsUpNb = 0;
	violetGemsUpNb = 0;

}

/*Envoie les gemmes au client pour initialisation (position)*/
void ServerWorldObjects::firstSend() {	

	/*On envoie d'abord le nombre de gems jaunes*/
	socketPlayer1->send(&yellowGemsNumber, sizeof(int));
	socketPlayer2->send(&yellowGemsNumber, sizeof(int));

	/*Envoie ensuite les gems jaunes*/
	for(int i = 0; i < yellowGemsNumber; i++) {
		yellowGems[i]->send(*socketPlayer1);
		yellowGems[i]->send(*socketPlayer2);
	}

	/*On envoie ensuite le nombre de gems viollette*/
	socketPlayer1->send(&violetGemsNumber, sizeof(int));
	socketPlayer2->send(&violetGemsNumber, sizeof(int));

	/*Recoit ensuite les gems viollette*/
	for(int i = 0; i < violetGemsNumber; i++) {
		violetGems[i]->send(*socketPlayer1);
		violetGems[i]->send(*socketPlayer2);
	}

}

